//
//  QianBaoTiViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/9.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "QianBaoTiViewController.h"
#import "QRCodeScanViewController.h"
#import "mentionViewController.h"
#import "taskOverviewService.h"
#import "taskUserManagerService.h"
#import "HomePassWordView.h"
#import <Masonry.h>
#import "MeModifyViewController.h"
#import "viewPayValidate.h"
#import "MeAuthenticationViewController.h"
#import "MeSecurityViewController.h"
#import "dataTransferDInfo.h"
//dataTransferDInfo dataUserValidateInfo
@interface QianBaoTiViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *recChainField;
@property (weak, nonatomic) IBOutlet UITextField *transferCountField;
@property (weak, nonatomic) IBOutlet UILabel *lbfee;
@property (weak, nonatomic) IBOutlet UILabel *lbdestCount;
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
@property (weak, nonatomic) IBOutlet UILabel *lbtitle;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic,strong)HomePassWordView *viewPassword;
@property (nonatomic,strong)viewPayValidate  *viewPayValid;

@property (nonatomic,strong)NSString *gteCode;
@property (nonatomic,assign)CGFloat  percent;

@end

@implementation QianBaoTiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title =@"转账";
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSliderAction)]];
    [self loadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setBlackStatus:YES];
    if (self.navigationController.navigationBar.hidden == YES) {
        self.navigationController.navigationBar.hidden = NO;
    }
}

-(void)loadData{
    [taskOverviewService getServiceChargePercent:nil success:^(dataCodeInfo *response, BOOL success) {
        if (success) {
            self.percent = [response.data floatValue]/100;
        }
    } failture:^(NSError *error, NSString *strDescription) {
        
    }];
}

-(viewPayValidate*)viewPayValid{
    if (!_viewPayValid ){
        _viewPayValid = [[[NSBundle mainBundle] loadNibNamed:@"viewPayValidate" owner:nil options:nil] objectAtIndex:0];
        [_viewPayValid initUI];
        [self.view addSubview:_viewPayValid];
        _viewPayValid.hidden = YES;
        [_viewPayValid mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.mas_equalTo(self.view);
            make.top.offset(60);
            make.bottom.mas_equalTo(self.view).offset(0);
        }];
        [_viewPayValid.btnSubmit addTarget:self action:@selector(btnSubmitClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _viewPayValid.labelNo3.hidden = YES;
        _viewPayValid.imgNo3.hidden = YES;
    }
    return _viewPayValid;
}

///用户验证点击完成
-(void)btnSubmitClick:(UIButton*)sender{
    if (![Check checkLenght:[UserCenter shareCenter].user.WithdrawPwd]) {
        //跳转到设置支付密码页面
        [self.navigationController pushViewController:[[MeModifyViewController alloc]initWithType:1] animated:YES];
    }else if ([[UserCenter shareCenter].user.IsIdentification isEqualToString:@"20060004"] || [[UserCenter shareCenter].user.IsIdentification isEqualToString:@"20060003"]) {
        [self.navigationController pushViewController:[MeAuthenticationViewController new] animated:YES];
        return;
    }
//    else if(!self.modelUserValidate.safeVerify){
//        //跳转到安全认证页面
//        [self.navigationController pushViewController:[MeSecurityViewController new] animated:YES];
//    }
}

-(BOOL) isValidateSuccess{
    if ([[UserCenter shareCenter].user.IsIdentification isEqualToString:@"20060001"]){
        [DKProgressHUD showErrorWithStatus:@"实名认证成功后才可以操作"];
        return NO;
    }
    
    self.viewPayValid.hidden = YES;
    if (![[UserCenter shareCenter].user.IsIdentification isEqualToString:@"20060002"] || ![Check checkLenght:[UserCenter shareCenter].user.WithdrawPwd]) {
        self.viewPayValid.hidden = NO;
        
        //隐藏安全验证信息
        self.viewPayValid.labelNo3.hidden = YES;
        self.viewPayValid.imgNo3.hidden = YES;
        UIColor *colorF_no = LCFromRGB(102, 102, 102);

        self.viewPayValid.labelNo1.textColor = colorF_no;
        self.viewPayValid.labelNo2.textColor = colorF_no;

        NSString *str = @"实名认证";
        if(![Check checkLenght:[UserCenter shareCenter].user.WithdrawPwd]){
            str = @"设置支付密码";
        }
        [self.viewPayValid.btnSubmit setTitle:str forState:UIControlStateNormal];
        
        return NO;
    }
    return YES;
}


#pragma mark - init
-(HomePassWordView*)viewPassword{
    if (!_viewPassword ){
        _viewPassword = [[[NSBundle mainBundle] loadNibNamed:@"HomePassWordView" owner:nil options:nil] objectAtIndex:0];
        [_viewPassword initUI];
        [self.view addSubview:_viewPassword];
        _viewPassword.hidden = YES;
        [_viewPassword mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.mas_equalTo(self.view);
            make.top.offset(60);
            make.bottom.mas_equalTo(self.view).offset(0);
        }];
        [_viewPassword.btnNext setTitle:@"确认提币" forState:UIControlStateNormal];
    }
    return _viewPassword;
}

- (IBAction)gteAction:(id)sender {
    
    if (![self isValidateSuccess]) {
        return;
    }
    
    if (![Check checkLenght:self.recChainField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输提币地址"];
        return;
    }
    if ([self.recChainField.text isEqualToString:self.gteCode]) {
        [DKProgressHUD showErrorWithStatus:@"不能输入自己的提币地址"];
        return;
    }
    if (![Check IsABChinese:self.transferCountField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输有效的提币数量"];
        return;
    }
    if (!([self.transferCountField.text floatValue] >= 20)) {
        [DKProgressHUD showErrorWithStatus:@"需至少提现20china-A"];
        return;
    }
    
    if ([self.transferCountField.text floatValue] > [self.coinModel.AvailableBurseCoinNumber floatValue]) {
        [DKProgressHUD showErrorWithStatus:@"china-A不足，请充值"];
        return;
    }
        ///进入输入密码的模块
        self.viewPassword.hidden = NO;
        __block QianBaoTiViewController *  blockSelf = self;
        self.viewPassword.blockSubmit = ^(NSString *pwd) {
            [blockSelf getTransferload:pwd];
        };
}

-(void)getTransferload:(NSString *)payPassword{
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setField:self.transferCountField.text value:@"transferAccountsNum"];//转账数量
    [dict setField:self.recChainField.text value:@"transferAccountsUser"]; //转账地址
    [DKProgressHUD showLoading];
    [taskOverviewService getTransfer:dict success:^(BaseModel *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            [DKProgressHUD showErrorWithStatus:@"提币成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

- (IBAction)codeAction:(id)sender {
    QRCodeScanViewController *vc = [QRCodeScanViewController new];
    __weak __typeof(self)weakSelf = self;
    vc.blockQR = ^(NSString *strQR) {
        weakSelf.recChainField.text = strQR;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.view endEditing:YES];
    if ([Check IsABChinese:self.transferCountField.text]) {
        self.lbfee.text =[NSString stringWithFormat:@"%.2f china-A",[self.transferCountField.text floatValue]* self.percent];
        self.lbdestCount.text = [NSString stringWithFormat:@"%.2f china-A",[self.transferCountField.text floatValue]];
//        self.lbdestCount.text = [NSString stringWithFormat:@"%.2f china-A",[self.transferCountField.text floatValue] - [self.transferCountField.text floatValue]* self.percent];
    }
}

-(void)tapSliderAction
{
    [self.view endEditing:YES];
}

- (IBAction)btnAll:(id)sender {
    self.transferCountField.text = self.coinModel.AvailableBurseCoinNumber;
    self.lbfee.text = [NSString stringWithFormat:@"%.2f china-A",[self.transferCountField.text floatValue] * self.percent];
    self.lbdestCount.text = [NSString stringWithFormat:@"%.2f china-A",[self.transferCountField.text floatValue]];
//    self.lbdestCount.text = [NSString stringWithFormat:@"%.2f china-A",[self.coinModel.AvailableBurseCoinNumber floatValue]- [self.transferCountField.text floatValue] * self.percent];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
